package com.sg.service.biz.app.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.app.req.AutoDeleteAddEvaTempDataComReqDto;
import com.sg.dto.biz.app.req.ImplementAcceptFieldReqDto;
import com.sg.dto.biz.app.req.ImplementCleaningWasteEvaluationDataComReqDto;
import com.sg.dto.biz.app.res.AutoDeleteAddEvaTempDataComRespDto;
import com.sg.dto.biz.app.res.ImplementAcceptFieldRespDto;
import com.sg.dto.biz.app.res.ImplementCleaningWasteEvaluationDataComRespDto;
import com.sg.service.base.app.MOmsEvaluationObjectTargetCycleService;
import com.sg.service.base.app.MOmsEvaluationTemplateService;
import com.sg.service.biz.app.SystemTestDomainService;
import com.sg.common.exception.Assert;
import com.wicket.okrcomponent.integration.InterfaceModeService;
import com.wicket.okrcomponent.integration.FwCompOperationsDeploymentClient;
import com.wicket.okrcomponent.integration.dto.*;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class OperationsDeploymentServiceImpl
        implements com.wicket.okrapp.biz.service.OperationsDeploymentService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MOmsEvaluationTemplateService mOmsEvaluationTemplateService;
    @Resource
    private MOmsEvaluationObjectTargetCycleService mOmsEvaluationObjectTargetCycleService;
    //@Resource
    //private undefinedService undefinedService;
    @Resource
    private NbOperationsDeployment nbOperationsDeployment;
    @Resource
    private FwCompOperationsDeploymentClient fwCompOperationsDeploymentClient;
    @Resource
    private InterfaceModeService interfaceModeService;
    @Resource
    private SystemTestDomainService systemTestDomainService;

    /**
     * D3执行清洗垃圾评价数据(公共)[7192]
     * gen by moon at 7/18/2023, 2:42:26 PM
     */
    @Trace(operationName = "D3执行清洗垃圾评价数据(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementCleaningWasteEvaluationDataComRespDto implementCleaningWasteEvaluationDataCom(ImplementCleaningWasteEvaluationDataComReqDto reqDto) {


        ImplementAcceptFieldRespDto receptionServiceRes_1 = null;
//virtualUsage D2执行动态寻表数据(公共)  45231
        ImplementDynamicLookupDataComRespDto implementDynamicLookupDataComRespDto = null;
        ImplementDynamicLookupDataComReqDto implementDynamicLookupDataComReqDto = new ImplementDynamicLookupDataComReqDto();
        if (reqDto != null) {
            implementDynamicLookupDataComReqDto.setToolTableEncryptionName(reqDto.getToolTableEncryptionName());//SimpleFieldAssign//sourceId:1035227_1_45231
            implementDynamicLookupDataComReqDto.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:1035228_1_45231
            implementDynamicLookupDataComReqDto.setThemeContentId(reqDto.getThemeContentId());//SimpleFieldAssign//sourceId:1035229_1_45231
            implementDynamicLookupDataComReqDto.setCycleDataObjId(reqDto.getCycleDataObjId());//SimpleFieldAssign//sourceId:1035230_1_45231
        }

        /*D2执行动态寻表数据(公共)[7186]   */
        Assert.isNull(implementDynamicLookupDataComReqDto.getToolTableEncryptionName(), "D3执行清洗垃圾评价数据(公共)-D2执行动态寻表数据(公共)-设计工具加密表名不能为空", false);
        implementDynamicLookupDataComRespDto = fwCompOperationsDeploymentClient.implementDynamicLookupDataCom(implementDynamicLookupDataComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


//virtualUsage D2执行数据集记录去重(公共)  45232
        ImplementDataSetDuplicateRemovalComRespDto implementDataSetDuplicateRemovalComRespDto = null;
        if (implementDynamicLookupDataComRespDto != null) {
            ImplementDataSetDuplicateRemovalComReqDto implementDataSetDuplicateRemovalComReqDto = new ImplementDataSetDuplicateRemovalComReqDto();
            if (implementDynamicLookupDataComRespDto != null && implementDynamicLookupDataComRespDto.getDynamicLookupDataList() != null && !CollectionUtil.isEmpty(implementDynamicLookupDataComRespDto.getDynamicLookupDataList())) {
                implementDataSetDuplicateRemovalComReqDto.setDataSetDuplicateRemovalList(implementDynamicLookupDataComRespDto.getDynamicLookupDataList().stream().map(item -> item.getEvaluationTemplateId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:1035237_1_45232
            }

            /*D2执行数据集记录去重(公共)[5138]   */

            implementDataSetDuplicateRemovalComRespDto = interfaceModeService.implementDataSetDuplicateRemovalCom(implementDataSetDuplicateRemovalComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        }
//virtualUsage 3-2-02查评价模板列表  45233
        List<OmsEvaluationTemplate> listOmsEvaluationTemplate = new ArrayList<>();
        QueryEvaTempListReq queryEvaTempListReq = new QueryEvaTempListReq();
        queryEvaTempListReq.setIsArchive("FALSE");//sourceId:1035776_1_45233

        /*3-2-02查评价模板列表[2191]   */
        Assert.isNull(queryEvaTempListReq.getIsArchive(), "D3执行清洗垃圾评价数据(公共)-3-2-02查评价模板列表-是否存档不能为空", false);
        listOmsEvaluationTemplate = mOmsEvaluationTemplateService.queryEvaTempList(queryEvaTempListReq)/*vcase invoke 本地 method 方法调用;*/;


//virtualUsage D2执行数据集差集(公共)  45236
        ImplementCollectionsDiffComRespDto implementCollectionsDiffComRespDto = null;
        if (implementDataSetDuplicateRemovalComRespDto != null && listOmsEvaluationTemplate != null && !CollectionUtil.isEmpty(listOmsEvaluationTemplate) && listOmsEvaluationTemplate.size() > 0) {
            ImplementCollectionsDiffComReqDto implementCollectionsDiffComReqDto = new ImplementCollectionsDiffComReqDto();
            if (implementDataSetDuplicateRemovalComRespDto != null) {
                implementCollectionsDiffComReqDto.setCollectionsDiffComFatList(implementDataSetDuplicateRemovalComRespDto.getDataSetDuplicateRemovalList());//list-field-assign//sourceId:1035819_1_45236
            }
            if (listOmsEvaluationTemplate != null && !CollectionUtil.isEmpty(listOmsEvaluationTemplate) && listOmsEvaluationTemplate != null && !CollectionUtil.isEmpty(listOmsEvaluationTemplate)) {
                implementCollectionsDiffComReqDto.setCollectionsDiffComSubList(listOmsEvaluationTemplate.stream().map(item -> item.getEvaluationTemplateId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:1035818_1_45236
            }

            /*D2执行数据集差集(公共)[7197]   */

            implementCollectionsDiffComRespDto = interfaceModeService.implementCollectionsDiffCom(implementCollectionsDiffComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        }
//virtualUsage M3接收评价模板列表  45237
        //ModelCode: receptionService
        ImplementAcceptFieldRespDto receptionServiceRes = null;
        if (implementCollectionsDiffComRespDto != null) {
            ImplementAcceptFieldReqDto receptionServiceReq = new ImplementAcceptFieldReqDto();
            if (reqDto != null) {
                receptionServiceReq.setTureOrFalse(reqDto.getTureOrFalse());//SimpleFieldAssign//sourceId:1035780_1_45237
                receptionServiceReq.setCreateInductionId(reqDto.getCreateInductionId());//SimpleFieldAssign//sourceId:1035777_1_45237
                receptionServiceReq.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:1035778_1_45237
                receptionServiceReq.setAppId(reqDto.getAppId());//SimpleFieldAssign//sourceId:1035779_1_45237
            }
            if (implementCollectionsDiffComRespDto != null) {
                receptionServiceReq.setEvalTempList(implementCollectionsDiffComRespDto.getCollectionsDiffList());//list-field-assign//sourceId:1035697_1_45237
            }

            /*M3接收评价模板列表[6100]  用于特殊方法接收上游入参。 */
            Assert.isNull(receptionServiceReq.getTureOrFalse(), "D3执行清洗垃圾评价数据(公共)-M3接收评价模板列表-是否不能为空", false);
            Assert.isNull(receptionServiceReq.getCreateInductionId(), "D3执行清洗垃圾评价数据(公共)-M3接收评价模板列表-创建人就职记录ID 不能为空", false);
            Assert.isNull(receptionServiceReq.getSpaceId(), "D3执行清洗垃圾评价数据(公共)-M3接收评价模板列表-创建于空间ID不能为空", false);
            Assert.isNull(receptionServiceReq.getAppId(), "D3执行清洗垃圾评价数据(公共)-M3接收评价模板列表-创建于联盟应用ID不能为空", false);
            receptionServiceRes = nbOperationsDeployment.implementAcceptField(receptionServiceReq);


            receptionServiceRes_1 = receptionServiceRes;
        }
        if ((receptionServiceRes != null && receptionServiceRes.getEvalTempList() != null && receptionServiceRes.getEvalTempList().size() > 0 && reqDto != null && reqDto.getTureOrFalse() != null && reqDto.getTureOrFalse().equals("TRUE"))) {
            //if((M3接收评价模板列表.评价模板列表数据集条数 大于 0 and D3执行清洗垃圾评价数据(公共).是否 等于 是))  45238

//ModelCode: circulationCollections
            for (String circulationCollectionsRes : receptionServiceRes.getEvalTempList()) {

                AutoDeleteAddEvaTempDataComRespDto autoDeleteAddEvaTempDataComRespDto = null;
                AutoDeleteAddEvaTempDataComReqDto autoDeleteAddEvaTempDataComReqDto = new AutoDeleteAddEvaTempDataComReqDto();
                if (circulationCollectionsRes != null) {
                    autoDeleteAddEvaTempDataComReqDto.setEvaluationTemplateId(circulationCollectionsRes);//SimpleFieldAssign//sourceId:1035272_1_45241
                }
                if (reqDto != null) {
                    autoDeleteAddEvaTempDataComReqDto.setCreateInductionId(reqDto.getCreateInductionId());//SimpleFieldAssign//sourceId:1035273_1_45241
                    autoDeleteAddEvaTempDataComReqDto.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:1035274_1_45241
                    autoDeleteAddEvaTempDataComReqDto.setAppId(reqDto.getAppId());//SimpleFieldAssign//sourceId:1035275_1_45241
                }

                /*D3-一键删除评价模板验证数据(公共)[4207]   */
//    Assert.isNull(autoDeleteAddEvaTempDataComReqDto.getEvaluationTemplateId(),"D3执行清洗垃圾评价数据(公共)-D3-一键删除评价模板验证数据(公共)-评价模板ID不能为空",false);
//Assert.isNull(autoDeleteAddEvaTempDataComReqDto.getCreateInductionId(),"D3执行清洗垃圾评价数据(公共)-D3-一键删除评价模板验证数据(公共)-创建人就职记录ID 不能为空",false);
//Assert.isNull(autoDeleteAddEvaTempDataComReqDto.getSpaceId(),"D3执行清洗垃圾评价数据(公共)-D3-一键删除评价模板验证数据(公共)-创建于空间ID不能为空",false);
//Assert.isNull(autoDeleteAddEvaTempDataComReqDto.getAppId(),"D3执行清洗垃圾评价数据(公共)-D3-一键删除评价模板验证数据(公共)-创建于联盟应用ID不能为空",false);
//      autoDeleteAddEvaTempDataComRespDto = systemTestDomainService.autoDeleteAddEvaTempDataCom(autoDeleteAddEvaTempDataComReqDto)/*vcase invoke isSameApp*/;


//ModelCode: circulationEnd
            }

        }
        ImplementCleaningWasteEvaluationDataComRespDto retData = new ImplementCleaningWasteEvaluationDataComRespDto();
        if (receptionServiceRes_1 != null) {
            retData.setCleaningWasteEvaluationDataList(receptionServiceRes_1.getEvalTempList());//list-field-assign//sourceId:1035785_1
        }


        return retData;
    }
    //
}
